//
//  EXCELViewController.m
//  YiFei
//
//  Created by yangyan on 16/9/23.
//  Copyright © 2016年 yous. All rights reserved.
//

#import "EXCELViewController.h"

@interface EXCELViewController ()<UIDocumentInteractionControllerDelegate>
@property(nonatomic,strong)NSArray  *nameArray;
@property(nonatomic,strong)NSArray  *sexArray;
@property(nonatomic,strong)NSArray  *schoolArray;
@property(nonatomic,strong)NSArray  *phoneArray;


@end

@implementation EXCELViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self addButton];
}


-(void)addButton{
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(30, 60, 100, 30);
    [button addTarget:self action:@selector(clickBarButton) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor=[UIColor blueColor];
    [self.view addSubview:button];
}

-(void)clickBarButton{
    NSLog(@"createExcel");
    
//    BookHandle book = xlCreateBook(); // use xlCreateXMLBook() for working with xlsx files
//    
//    SheetHandle sheet = xlBookAddSheet(book, "Sheet1", NULL);
//    //第一个参数代表插入哪个表，第二个是第几行（默认从0开始），第三个是第几列（默认从0开始）
//    xlSheetWriteStr(sheet, 1, 0, "姓名", 0);
//    xlSheetWriteStr(sheet, 1, 1, "性别", 0);
//    xlSheetWriteStr(sheet, 1, 2, "学校", 0);
//    xlSheetWriteStr(sheet, 1, 3, "电话", 0);
//    
//    
//    for (int i = 0; i < self.nameArray.count; i++) {
//        const char *name_c = [self.nameArray[i] cStringUsingEncoding:NSUTF8StringEncoding];  //这里是将NSString字符串转为C语言字符串
//        xlSheetWriteStr(sheet, i+2, 0,name_c, 0);
//        
//    }
//    for (int i = 0; i < self.sexArray.count; i++) {
//        const char *sex_c = [self.sexArray[i] cStringUsingEncoding:NSUTF8StringEncoding];
//        xlSheetWriteStr(sheet, i+2, 1,sex_c, 0);
//        
//    }
//    for (int i = 0; i < self.schoolArray.count; i++) {
//        const char *school_c = [self.schoolArray[i] cStringUsingEncoding:NSUTF8StringEncoding];
//        xlSheetWriteStr(sheet, i+2, 2,school_c, 0);
//        
//    }
//    for (int i = 0; i < self.phoneArray.count; i++) {
//        const char *phone_c = [self.phoneArray[i] cStringUsingEncoding:NSUTF8StringEncoding];
//        xlSheetWriteStr(sheet, i+2, 3,phone_c, 0);
//    }
//    NSString *documentPath =
//    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
//    NSString *filename = [documentPath stringByAppendingPathComponent:@"out.xls"];
//    NSLog(@"filepath--%@",filename);
//    
//    xlBookSave(book, [filename UTF8String]);
//    
//    xlBookRelease(book);
//    
//    //导出xls文件
//    UIDocumentInteractionController *docu = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filename]];
//    
//    docu.delegate = self;
//    CGRect rect = CGRectMake(0, 0, 320, 300);  //这里感觉没什么用
//    
//    [docu presentOpenInMenuFromRect:rect inView:self.view animated:YES];  //不写可以直接预览
//    
//    [docu presentPreviewAnimated:YES];  //这句比较坑爹。如果不写这句，只写上面那句会弹出选择支持xls文件的APP。但是如果没写程序就会崩了，还有一个<p class="p1"><span class="s1">                                        //[docu </span><span class="s2">presentOptionsMenuFromRect</span><span class="s1">:rect </span><span class="s2">inView</span><span class="s1">:</span><span class="s3">self</span><span class="s1">.</span><span class="s4">view</span><span class="s1"> </span><span class="s2">animated</span><span class="s1">:</span><span class="s3">YES</span><span class="s1">];可以研究下。</span></p>
}

- ( UIViewController *)documentInteractionControllerViewControllerForPreview:( UIDocumentInteractionController *)interactionController{
    
    return self;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
